#include "lrcpage.h"
#include "ui_lrcpage.h"
#include <QFile>
#include <QDebug>

LrcPage::LrcPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LrcPage)
{
    ui->setupUi(this);

    //设置下拉按钮图片
    ui->hideBtn->setIcon(QIcon(":/images/xiala.png"));

    //设置字体格式
    QFont font;
    font.setFamily("Microsoft YaHei"); // 字体名（可换成“微软雅黑”、“SimSun”等）
    font.setPixelSize(17);             // 字体大小（像素）
    font.setBold(false);                // 是否加粗
    ui->musicName->setFont(font);

    font.setPixelSize(12);
    ui->musicSinger->setFont(font);

    font.setPixelSize(20);
    ui->line1->setFont(font);
    ui->line2->setFont(font);
    ui->line3->setFont(font);
    ui->line4->setFont(font);
    ui->line5->setFont(font);
    ui->line6->setFont(font);

    font.setPixelSize(30);
    ui->lineCenter->setFont(font);

    //设置下拉按钮动画
    lrcPageAnimal = new QPropertyAnimation(this,"geometry",this);
    lrcPageAnimal->setDuration(250);
    lrcPageAnimal->setStartValue(QRect(0,0,width(),height()));
    lrcPageAnimal->setEndValue(QRect(0,height(),width(),height()));

    //下拉按钮关联
    connect(ui->hideBtn,&QPushButton::clicked,this,[&](){
        lrcPageAnimal->start();
    });

    //动画结束事件拦截
    connect(lrcPageAnimal,&QPropertyAnimation::finished,this,[&](){
        this->hide();
    });

}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrcFile(QString lrcPath)
{
    //先清空
    lrcWordLines.clear();

    QFile file(lrcPath);
    if(!file.open(QIODevice::ReadOnly)){
        qDebug() << "打开文件 "<<lrcPath<<" 失败\n";
        return false;
    }

    while(!file.atEnd()){
        QString lrcWord = file.readLine(1024);

        //lrcWord: "[02:39.22]最美不过头顶彩虹的村庄"
        //解析这一句歌词
        //解析时间 , 转换成毫秒
        qint64 lineTime = 0;

        int start = 1, end = 0;
        end = lrcWord.indexOf(':',start);
        lineTime += lrcWord.mid(start,end-start).toInt()*60*1000;

        start = end+1;
        end = lrcWord.indexOf('.',start);
        lineTime += lrcWord.mid(start,end-start).toInt()*1000;

        start = end+1;
        end = lrcWord.indexOf(']',start);
        lineTime += lrcWord.mid(start,end-start).toInt();

        start = end+1;
        lrcWordLines.push_back({lineTime,lrcWord.mid(start,lrcWord.size()-start-1)});
    }

    ////debug
    // for(auto& it : lrcWordLines){
    //     qDebug()<<it.first << " " << it.second<<endl;
    // }
    return true;
}

int LrcPage::getLrcWordLiesIdex(qint64 time){
    if(lrcWordLines.empty()){
        return -1;
    }

    for(int i=1;i<lrcWordLines.size();++i){
        if(lrcWordLines[i-1].first<=time && lrcWordLines[i].first>time){
            return i-1;
        }
    }

    return lrcWordLines.size()-1;
}

void LrcPage::clearLrcWord()
{
    ui->line1->clear();
    ui->line2->clear();
    ui->line3->clear();
    ui->lineCenter->clear();
    ui->line4->clear();
    ui->line5->clear();
    ui->line6->clear();
}

void LrcPage::showLrcWord(qint64 time)
{
    int idx = getLrcWordLiesIdex(time);
    setLine(ui->line1,idx-3);
    setLine(ui->line2,idx-2);
    setLine(ui->line3,idx-1);
    setLine(ui->lineCenter,idx);
    setLine(ui->line4,idx+1);
    setLine(ui->line5,idx+2);
    setLine(ui->line6,idx+3);
}

void LrcPage::setLine(QLabel *label, int idx)
{
    if(idx>=0 && idx<lrcWordLines.size()){
        label->setText(lrcWordLines[idx].second);
    }
}

void LrcPage::setMusicName(const QString &text)
{
    ui->musicName->setText(text);
}

void LrcPage::setMusicSinger(const QString &text)
{
    ui->musicSinger->setText(text);
}






